﻿/* Autoduel Game
 * CS4911 Senior Design Project
 * Fall 2010 Semester
 * Ian Axelrod, Liz Ha, Andrew Muldowney, Edward Futch
 */
using System.Collections.Generic;

using Microsoft.Xna.Framework;
using AutoduelClient.GameClient.Player;
using System;
using AutoduelClient.GameUI;
using AutoduelClient.GameClient.Locations;
using System.Windows.Forms;

namespace AutoduelClient.GameClient.Locations
{
    
    public delegate void ChassisListEventHandler(object sender, ChassisListEventArgs e);
    public delegate void RumorsListEventHandler(object sender, RumorsListEventArgs e);

    /// <summary>
    /// Represents a truck stop location in autoduel
    /// </summary>
    public class TruckStop : Location
    {
		#region Static Fields (1)
		// menu options given to game client
        private MenuChoice[] menu;
        #endregion
        #region Fields (1)
        // list of rumors at this truck stop
        private string[] rumors;

        /// <summary>
        /// The chasses available at truckstops
        /// </summary>
        public static List<Chassis> Chasses = new List<Chassis>();

        public event ChassisListEventHandler ChassisListRequested;
        public event RumorsListEventHandler RumorsListRequested;

		#endregion

		#region Properties (1)
		/// <summary>
        /// Gets/Sets the list of rumors at this truck stop
        /// </summary>
        public string[] Rumors
        {
            get { return rumors; }
            set { rumors = value; }
        }

        private Player.Player player;
		#endregion

		#region Methods (7)


        public TruckStop()
        {
            menu = new MenuChoice[] { 
            new MenuChoice(BuyArmor, "Purchase Armor", null, false), 
            new MenuChoice(CityBus, "City Bus", new Type[] { typeof (Coordinate) }, true),
            new MenuChoice(Heal, "Heal Self", null, false), 
            new MenuChoice(RechargeCar, "Recharge Car", null, false),
            new MenuChoice(RepairCar, "Repair Car", null, false),
            new MenuChoice(GetChasses, "Get Chasses", null, true),
            new MenuChoice(GetRumors, "Get Rumors", null, false) };
        }

        public override void Enter(Player.Player player)
        {
            player.CurrentLocation = this;
            player.InLocation = true;
            player.Client.SessionSend(player.EncodePacket(OPCODES.ORD_UPDPLAYER));
            this.player = player;
        }

        public override void Exit()
        {
            this.player.CurrentLocation = null;
            this.player.InLocation = false;
            this.player.Move(new Coordinate(-50, 0));
        }

		/// <summary>
        /// Buy armor for the player's car
        /// </summary>
        /// <param name="player">The player buying the armor</param>
        private void BuyArmor(object sender, MenuChoiceEventArgs e)
        {
            // Determine if we were in a car when we entered this location or not
            if (this.player.CurrentCar != null)
            {
                this.player.CurrentCar.Armor += 1;
            }
            else
            {
                this.player.Health += 1;
            }

            this.player.Credits -= 100; // arbitrary value right now
        }

		/// <summary>
        /// Takes the player to a desired position in the
        /// autoduel world
        /// </summary>
        private void CityBus(object sender, MenuChoiceEventArgs e)
        {
            ((Form)sender).Owner.Close();

            this.player.CurrentLocation = null;
            this.player.CurrentCoord = (Coordinate)e.menuChoice.data[0];
            this.player.Client.SessionSend(this.player.EncodePacket(OPCODES.ORD_UPDPLAYER));
        }

		/// <summary>
        /// Sets the player to full health
        /// </summary>
        private void Heal(object sender, MenuChoiceEventArgs e)
        {
            this.player.Health = Player.Player.MAX_HEALTH;
        }

		/// <summary>
        /// Sets the current car's battery back to a full charge
        /// </summary>
        private void RechargeCar(object sender, MenuChoiceEventArgs e)
        {
            if (this.player.CurrentCar != null)
            {
                this.player.Credits -= 100; // arbitary value right now
                this.player.CurrentCar.PowerPlant.CurrentBatteryCharge = player.CurrentCar.PowerPlant.MaxBatteryCharge;
            }
        }

		/// <summary>
        /// Repairs the player's current car
        /// </summary>
        private void RepairCar(object sender, MenuChoiceEventArgs e)
        {
            if (this.player.CurrentCar != null)
            {
                this.player.Credits -= 200; // arbitary value right now
                this.player.CurrentCar.Armor = player.CurrentCar.MaxArmor;
            }
        }

        private void GetChasses(object sender, MenuChoiceEventArgs e)
        {
            ChassisListRequested(this, new ChassisListEventArgs(Chasses.ToArray()));
        }

        private void GetRumors(object sender, MenuChoiceEventArgs e)
        {
            RumorsListRequested(this, new RumorsListEventArgs(rumors));
        }

		/// <summary>
        /// Provide the menu contents
        /// </summary>
        /// <returns>The menu options for truck stop</returns>
        public override MenuChoice[] ProvideMenuChoices()
        {
            return menu;
        }

        /// <summary>
        /// Provides a string representation of the TruckStop
        /// </summary>
        /// <returns>The string representation of the truckstop</returns>
        public override string ToString()
        {
            return "Name: " + this.Name + " -- Entrance: " + this.Entrance;
        }
		#endregion
    }
}
